package code.example.base.util.concurrent.threadstate;

import code.example.tool.LogFactory;
import code.example.tool.SleepUtils;
import org.junit.Test;

public class SynchronizedWaitState {
    private final Object obj = new Object();
    public Thread startOneThread(final Thread mainThread){
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (obj){
                    Thread.State state =  mainThread.getState();
                    LogFactory.getLog().info("main thead state:"+state);
                    obj.notifyAll();
                     state =  mainThread.getState();
                    LogFactory.getLog().info("main thead state:"+state);
                }


                    Thread.State state =  mainThread.getState();
                    LogFactory.getLog().info("main thead state:"+state);



            }
        },"RunningThread");
        t.start();
        return t;
    }

    @Test
    public void WaitOneThead(){

        try {
            synchronized (obj){
                startOneThread(Thread.currentThread());
                LogFactory.getLog().info("main thread wait start:");
                obj.wait();
                LogFactory.getLog().info("main thead state:"+Thread.currentThread().getState());
                LogFactory.getLog().info("main thread  wait finish!");
            }

        }catch (Exception e){
            LogFactory.getLog().error(e);
        }
    }
}
